package cn.nawang.ebeim.ui.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.client.bean.ClientConfiguration;
import cn.nawang.ebeim.ui.bean.Constant;
import cn.nawang.ebeim.ui.bean.UpdatePassword;
import cn.nawang.ebeim.ui.util.ApiUtil;
import cn.nawang.ebeim.ui.util.FileOprt;
import cn.nawang.ebeim.ui.util.MACAddress;
import cn.nawang.ebeim.ukey.Ukey;

/**
 * @ClassName: AuthorizationCheckController
 * @Description: TODO
 * @author fanyb
 * @date Oct 28, 2015 10:42:31 AM
 * @version v1.0
 */
@RestController
@RequestMapping(value = "/noIntercept")
public class AuthorizationCheckController {
  private static final Logger LOG = LoggerFactory.getLogger(AuthorizationCheckController.class);

  /**
   * 
   * @author fanyb
   * @Description: 根据用户名称获取短信验证码
   * @param username
   * @return String
   * @date Oct 28, 20151:06:07 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/short_message/send", method = RequestMethod.GET)
  public String getShortMessage(String username) {
    LOG.info("/short_message/check {}", username);
    String data = ApiUtil.getShortMessage(username);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: 带验证码验证的修改密码（首次登录强制修改）
   * @param updatePassword
   * @return String
   * @date Oct 28, 20151:06:43 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/short_message/update_password", method = RequestMethod.POST)
  public String updatePasswordShortMessageCheck(UpdatePassword updatePassword) {
    LOG.info("/short_message/update_password {}", updatePassword);
    String data = ApiUtil.updatePasswordShortMessageCheck(updatePassword);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: 无授权验证
   * @return String
   * @date Oct 28, 20151:13:52 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/no_authorization/check", method = RequestMethod.GET)
  public String noAuthorizetionCheck(String username) {
    LOG.info("/no_authorization/check {}");
    String data = ApiUtil.noAuthorizetionCheck(username);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: key无关联验证
   * @param username
   * @param code
   * @return String
   * @date Oct 28, 20151:42:37 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/key_no_bind/check", method = RequestMethod.GET)
  public String keyNoBindCheck(String username, String code, String bindDeviceName) {
    LOG.info("/no_authorization/check {}", username, code, bindDeviceName);
    String key = getUkeyValue();
    String data = ApiUtil.keyNoBindCheck(username, code, key);
    FileOprt.writeFileCover(ClientConfiguration.getWorkingPath() + File.separator + username,
        bindDeviceName);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: key关联验证
   * @param username
   * @param code
   * @param bindDevice
   * @param bindDeviceName
   * @return String
   * @date Oct 28, 20151:50:13 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/key_bind/check", method = RequestMethod.GET)
  public String keyBindCheck(String username, String code, String bindDevice,
      String bindDeviceName) {
    LOG.info("/key_bind/check {}", username, code, bindDevice, bindDeviceName);
    String key = getUkeyValue();
    String data = ApiUtil.keyBindCheck(username, code, bindDevice, bindDeviceName, key);
    writeBindDevice(username, bindDeviceName);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: 授权码不关联
   * @param username
   * @param code
   * @return String
   * @date Oct 28, 20152:22:04 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/authorization_code_no_bind/check", method = RequestMethod.GET)
  public String authorizationCodeNoBindCheck(String username, String code, String bindDeviceName) {
    LOG.info("/authorization_code_no_bind/check {}");
    String data = ApiUtil.authorizationCodeNoBindCheck(username, code);
    writeBindDevice(username, bindDeviceName);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: 授权码绑定验证
   * @param username
   * @param code
   * @param bindDevice
   * @param bindDeviceName
   * @return String
   * @date Oct 28, 20152:16:37 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/authorization_code_bind/check", method = RequestMethod.GET)
  public String authorizationCodeBindCheck(String username, String code, String bindDevice,
      String bindDeviceName) {
    LOG.info("/authorization_code_bind/check {}");
    String data = ApiUtil.authorizationCodeBindCheck(username, code, bindDevice, bindDeviceName);
    writeBindDevice(username, bindDeviceName);
    return data;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取本机的设备特征和设备名称
   * @return Map<String,Object>
   * @date Oct 28, 20153:08:06 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/computer_message/check", method = RequestMethod.GET)
  public Map<String, Object> getComputerMessage() {
    LOG.info("/test/computer_message/check {}");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("code", Constant.CODE_ERROR);
    String bindDeviceName = MACAddress.getComputerName();
    String bindDevice = MACAddress.getMACAddr();
    map.put("bindDevice", bindDevice);
    map.put("bindDeviceName", bindDeviceName);
    map.put("code", Constant.CODE_SUCCESS);
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取u盾值
   * @return String
   * @date Oct 28, 20155:20:17 PM
   *
   * @修改人
   * @修改日期
   */
  @RequestMapping(value = "/ukey/check", method = RequestMethod.GET)
  public String getUkeyValue() {
    LOG.debug("开始读取key值");
    Ukey.loadLibrary();
    String key = Ukey.getSerialId();
    LOG.info("UkeyValue {}", key);
    return key;
  }

  /**
   * 
   * @author fanyb
   * @Description: 写到绑定设备
   * @param username
   * @param bindDeviceName
   * @date Nov 4, 20156:56:28 PM
   *
   * @修改人
   * @修改日期
   */
  public void writeBindDevice(String username, String bindDeviceName) {
    FileOprt.writeFileCover(ClientConfiguration.getWorkingPath() + File.separator + username,
        bindDeviceName);
  }
  
  public static void main(String args[]){
    AuthorizationCheckController a=new AuthorizationCheckController();
    a.writeBindDevice("fanyb", "test");
  }
}
